package com.itbbs.email.service.impl;

import com.itbbs.email.service.api.EmailService;
import com.itbbs.email.utils.RandomUtil;
import com.itbbs.exception.CustomizeException;
import com.itbbs.redis.client.RedisFeignClient;
import com.itbbs.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 邮件服务业务层实现类
 * @createTime 2021年06月21日 11:01:00
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @Resource
    private RedisFeignClient redisFeignClient;

    @Value("${spring.mail.username}")
    private String sendName;

    @Override
    public void sendEmail(String emailAddress, String type) {
        // 为了方便程序的扩展使用反射
        try {
            Method method = this.getClass().getDeclaredMethod(type,String.class);
            method.setAccessible(true);
            method.invoke(this,emailAddress);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomizeException(ResultCodeEnum.FAILED_TO_SEND_MAIL);
        }
    }


    public void registeredEmail(String emailAddress){
        // 对邮件进行加密
        Map<String,Object> map = new HashMap<>();
        // 获取验证码
        String code = RandomUtil.getSixBitRandom();
        // 调用redis服务将数据存储到redis中间件中
        redisFeignClient.setSecond(emailAddress,code,3);
        // TODO 用来封装数据的
        // 验证码
        map.put("code", RandomUtil.getSixBitRandom());
        map.put("id","123456");
        // 调用模板发送邮件
        this.emailTemplate(emailAddress,map,"registered.html");
    }

    private void emailTemplate(String emailAddress,Map<String,Object> map,String templateName){
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("来自网站的信息");
            // 发件人
            helper.setFrom(sendName);
            // 收件人
            helper.setTo(emailAddress);
            // 时间
            helper.setSentDate(new Date());
            // 这里引入的是Template的Context
            Context context = new Context();
            // 设置模板中的变量
            context.setVariables(map);
            String process = templateEngine.process(templateName, context);
            // 第二个参数true表示这是一个html文本
            helper.setText(process,true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new CustomizeException(ResultCodeEnum.FAILED_TO_SEND_MAIL);
        }
    }
}
